from numpy import *
import glob
from pylab import *
from scipy import *
import matplotlib
from scipy.interpolate import interp1d


all = empty([0,100])
spectra = [line.rstrip('\n') for line in open('list.txt')]
for i in spectra:
 x,y = loadtxt(i, usecols = (0,1), unpack = True)
 fc = interp1d(x,y, kind = 'cubic')
 xnew = np.linspace(0.5, 0.9, num=100, endpoint=True)
 result = fc(xnew)
 plot(x, y, '-', color = 'lightgrey')
 all = append(all, [result], axis = 0)
ave = all.mean(axis=0)
sd = all.std(axis=0)


first_legend = legend(['individual spectra'], loc = 1)
ax = gca().add_artist(first_legend)

plot(xnew, ave, '-', color = 'k', label='average spectrum', linewidth=2 )
plot(xnew, ave+sd, '--', color = 'k', label = '1-sigma' )
plot(xnew, ave-sd, '--', color = 'k' )
legend(loc = 3, prop={'size':16}, markerscale = 3, numpoints = 1)

xlim(0.45,0.9)
ylim(0.80,1.25)

xlabel('Wavelength (micron)', size=15)
ylabel('Reflectance', size=15)
savefig('lops_spec.pdf', dpi = 300)
